<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="注册用户id" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="注册用户id"></el-input>
      </el-form-item>
          <el-form-item label="邀请用户手机" prop="inviteUserPhone">
        <el-input v-model="dataForm.inviteUserPhone" placeholder="邀请用户手机"></el-input>
      </el-form-item>
          <el-form-item label="邀请用户名" prop="inviteUserName">
        <el-input v-model="dataForm.inviteUserName" placeholder="邀请用户名"></el-input>
      </el-form-item>
          <el-form-item label="邀请用户id" prop="inviteUserId">
        <el-input v-model="dataForm.inviteUserId" placeholder="邀请用户id"></el-input>
      </el-form-item>
          <el-form-item label="邀请时间" prop="inviteTime">
        <el-input v-model="dataForm.inviteTime" placeholder="邀请时间"></el-input>
      </el-form-item>
          <el-form-item label="邀请成功获得金额" prop="balance">
        <el-input v-model="dataForm.balance" placeholder="邀请成功获得金额"></el-input>
      </el-form-item>
          <el-form-item label="是否注册" prop="isRegist">
        <el-input v-model="dataForm.isRegist" placeholder="是否注册"></el-input>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  userId: '',  inviteUserPhone: '',  inviteUserName: '',  inviteUserId: '',  inviteTime: '',  balance: '',  isRegist: '',  sort: '',  createTime: '',  updateTime: ''});

const rules = ref({
          userId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          inviteUserPhone: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          inviteUserName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          inviteUserId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          inviteTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          balance: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          isRegist: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          sort: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          createTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          updateTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/userinvite/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/userinvite", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
